<?php

/**
 * Description of SignupService
 *
 * @author Adam
 */
class AccessService {
    private static $instance = null;

    public static function getInstance() {
       if (!self::$instance instanceof self) {
         self::$instance = new self;
       }
       return self::$instance;
    }

    public function __destruct() {
    }

    public function getAccess($DBprefix,$userId,$pageId) {
        $userId = mysql_escape_string($userId);
        $pageId = mysql_escape_string($pageId);
        $query = "SELECT * FROM `".$DBprefix."access` WHERE `userid` = '$userId' AND `pageid` = '$pageid'";
        $queryResult = mysql_query($query);
        
        $access = new Access();
        if ($access_info = mysql_fetch_array($queryResult)){ 
            $access->setUserId( $access_info["userid"] );
            $access->setPageId( $access_info["pageid"] );
            $access->setPermission( $access_info["permission"] );
        }
        
        return $access;
    }

    public function getAll($DBprefix) {
        $query = "SELECT * FROM `" . $DBprefix . "access`";
        $queryResult = mysql_query($query);
        
        $acessArr = array();
        while ($access_info = mysql_fetch_array($queryResult)){ 
            $access = new Access();
            $access->setUserId( $access_info["userid"] );
            $access->setPageId( $access_info["pageid"] );
            $access->setPermission( $access_info["permission"] );
            $accessArr[] = $access;
        }
        
        return $accessArr;
    }

    public function getAllOrderLimit($DBprefix,$orderBy,$begin,$rowsOnPage) {
        $query = "SELECT * FROM `" . $DBprefix . "access` ORDER BY `$orderBy` DESC LIMIT $begin,$rowsOnPage";
        $queryResult = mysql_query($query);
        
        $acessArr = array();
        while ($access_info = mysql_fetch_array($queryResult)){ 
            $access = new Access();
            $access->setUserId( $access_info["userid"] );
            $access->setPageId( $access_info["pageid"] );
            $access->setPermission( $access_info["permission"] );
            $accessArr[] = $access;
        }
        
        return $accessArr;
    }

    public function count($DBprefix) {
        $query = "SELECT * FROM `" . $DBprefix . "access`";
        $queryResult = mysql_query($query);
        $count = mysql_num_rows($queryResult);
        return $count;
    }
}

?>
